﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Radovici.SharedLibrary;
using YouTubePlaylist.MusicVideoService;

namespace YouTubePlaylist.Models
{
    public class TrackInputModel : Notifiable
    {
        #region Private fields

        private readonly MusicVideoServiceClient _client =
            new MusicVideoServiceClient(
                new BasicHttpBinding {MaxBufferSize = 2147483647, MaxReceivedMessageSize = 2147483647},
                new EndpointAddress(DataModel.EndPoint + "MusicVideoService.svc"));

        private string _lastArtistsRequest;
        private string _lastArtistTracksRequest;
        private string _lastTracksRequest;

        #endregion

        public TrackInputModel()
        {
            _client.GetArtistsCompleted += OnGetArtistsCompleted;
            _client.GetArtistTracksCompleted += OnGetArtistTracksCompleted;
            _client.GetTracksCompleted += OnGetTracksCompleted;
        }

        public IEnumerable<string> Categories
        {
            get { return new List<string>(new[] {"karaoke", "live", "video", "lyrics", "remix"}); }
        }

        public IEnumerable<string> Artists { get; private set; }

        public IEnumerable<Track> ArtistTracks { get; private set; }

        public IEnumerable<string> ArtistTrackTitles
        {
            get { return ArtistTracks == null ? null : ArtistTracks.Select(t => t.Title); }
        }

        public IEnumerable<Track> Tracks { get; private set; }

        public IEnumerable<string> TrackTitles
        {
            get { return Tracks == null ? null : Tracks.Select(t => t.Title); }
        }

        public void GetArtistTracks(string artist, string track)
        {
            _lastArtistTracksRequest = artist + track;
            _client.GetArtistTracksAsync(DataModel.Singleton.User, artist, track, _lastArtistTracksRequest);
        }

        private void OnGetArtistTracksCompleted(object sender, GetArtistTracksCompletedEventArgs e)
        {
            if (e.Error == null && _lastArtistTracksRequest == e.UserState.ToString())
            {
                ArtistTracks = e.Result;
                Notify("ArtistTracks");
                Notify("ArtistTrackTitles");
            }
        }

        public void GetArtists(string artist)
        {
            _lastArtistsRequest = artist;
            _client.GetArtistsAsync(DataModel.Singleton.User, artist, _lastArtistsRequest);
        }

        private void OnGetArtistsCompleted(object sender, GetArtistsCompletedEventArgs e)
        {
            if (e.Error == null && _lastArtistsRequest == e.UserState.ToString())
            {
                Artists = e.Result;
                Notify("Artists");
            }
        }

        public void GetTracks(string track)
        {
            _lastTracksRequest = track;
            _client.GetTracksAsync(DataModel.Singleton.User, track, _lastTracksRequest);
        }

        private void OnGetTracksCompleted(object sender, GetTracksCompletedEventArgs e)
        {
            if (e.Error == null && _lastTracksRequest == e.UserState.ToString())
            {
                Tracks = e.Result;
                Notify("Tracks");
                Notify("TrackTitles");
            }
        }
    }
}